//
//  RegistViewController.m
//  hello mui
//
//  Created by zhangtao on 15/9/17.
//  Copyright (c) 2015年 zt.td. All rights reserved.
//

#import "RegistViewController.h"
#import "MainTabBarViewController.h"
#import "AppDelegate.h"
#import "AFNetworking.h"
@interface RegistViewController ()

@property (strong, nonatomic) IBOutlet UIView *phoneNumView;
@property (strong, nonatomic) IBOutlet UIView *psdView;
@property (strong, nonatomic) IBOutlet UIView *codeView;

@property (weak, nonatomic) IBOutlet UIButton *getYZM;
@property(nonatomic,assign)NSUInteger count;

@property (strong, nonatomic) IBOutlet UITextField *phoneNumText;
@property (strong, nonatomic) IBOutlet UITextField *psdText;
@property (strong, nonatomic) IBOutlet UITextField *codeText;

@property (strong, nonatomic) IBOutlet UIButton *submitBtn;





@property(nonatomic,strong)NSString *seeesionid;
@property(nonatomic,strong)NSString *code;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.count = 59;
    _phoneNumView.layer.cornerRadius = 9;
    _psdView.layer.cornerRadius = 9;
    _codeView.layer.cornerRadius = 9;
    _getYZM.layer.cornerRadius = 9;
    _submitBtn.layer.cornerRadius = 9;

    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)getYZM:(id)sender {
    NSString *path = @"login/getValidateCode";
    NSDictionary *dic = @{@"mobile":_phoneNum};
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
//    [manager POST:path parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"///%@",dic);
//        NSString *sessionId = [dic valueForKey:@"sessionid"];
//        _seeesionid = sessionId;
//        _code = [dic valueForKey:@"code"];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        //
//    }];
    [[HttpConnctionManager sharedInstance]requestParam:dic andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        _code = [succeedResult stringWithFilted:@"code"];
        _seeesionid = [succeedResult stringWithFilted:@"sessionId"];
        NSString *recode = [succeedResult valueForKey:@"resCode"];
        
        if ([recode isEqualToString:@"0"]) {   //get-->>success
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"验证码已发送至您的手机,请注意查收" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }else if([recode isEqualToString:@"1"]){    //get--->faile-->>///
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"对不起,系统错误，请稍后重试" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
            
            /**倒计时*/
            if(59 == self.count)
            {
                //        [self.getYZM setBackgroundColor:RGB(200, 201, 202, 1)];
                [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
            }

        }
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        
    }];
    
    
    
}
//59秒后才可再次获取验证码
- (void)countDown:(NSTimer *)timer
{
    [self.getYZM setTitle:[NSString stringWithFormat:@"%ld秒", (long)self.count] forState:UIControlStateNormal];
    self.count--;
    if(self.count <= 0)
    {
        [self.getYZM setBackgroundColor:[UIColor yellowColor]];
        [self.getYZM setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.getYZM setTitle:@" 获取验证码 " forState:UIControlStateNormal];
        [timer invalidate];
        self.count = 59;
    }
    
}


- (IBAction)submit:(id)sender {
    MainTabBarViewController *main = [self.storyboard instantiateViewControllerWithIdentifier:@"main"];
    if ([@"_YZMText.text" isEqualToString:_code]) {
        NSString *path = @"login/register";
        NSDictionary *dic = @{@"sessionid":_seeesionid,@"mobile":_phoneNum,@"code":_code};
        [[HttpConnctionManager sharedInstance]requestParam:dic andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
            
            if ([[succeedResult valueForKey:@"resCode"] isEqualToString:@"0"]) {
                //--success
                NSUSER_DEF_NORSET(@"selfLogin", @"SHARE");
                
                MainTabBarViewController *main = [self.storyboard instantiateViewControllerWithIdentifier:@"main"];
                [self presentViewController:main animated:NO completion:nil];
            }else{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"注册失败，请稍后重试" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }
            
            
        } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
            
        }];
        
        
    }
    
  
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
//    [manager POST:path parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"//register/%@",dic);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        //
//    }];

    
    
    [self presentViewController:main animated:NO completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
